package org.example.myleet.rosalind.spec;

import org.example.myleet.rosalind.utils.AminoAcidMassReader;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

public class Solution {

    private static final BigDecimal UPSILON = BigDecimal.valueOf(0.001);

    public static void main(String[] args) throws IOException {
        Solution solution = new Solution();
        String path = solution.getClass().getResource("").getPath() + "example.txt";
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        List<BigDecimal> prefixMass = new ArrayList<>();
        String line;
        while (null != (line = br.readLine())) {
            if (line.length() < 1) {
                break;
            }
            prefixMass.add(new BigDecimal(line.trim()));
        }
        prefixMass.sort(Comparator.naturalOrder());
        Map<String, BigDecimal> aminoAcidMapMass = AminoAcidMassReader.getAminoAcidMassMap();
        StringBuilder res = new StringBuilder();
        Iterator<BigDecimal> iterator = prefixMass.iterator();
        BigDecimal prev = iterator.next();
        while (iterator.hasNext()) {
            BigDecimal cur = iterator.next();
            BigDecimal diff = cur.subtract(prev);
            boolean found = false;
            for (Map.Entry<String, BigDecimal> entry : aminoAcidMapMass.entrySet()) {
                if (diff.subtract(entry.getValue()).abs().compareTo(UPSILON) < 0) {
                    res.append(entry.getKey());
                    found = true;
                    break;
                }
            }
            if (!found) {
                res.append('*');
            }
            prev = cur;
        }
        System.out.println(res);
        br.close();
    }
}
